package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlarmRecordDto;
import org.apache.ibatis.javassist.runtime.Desc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-21
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    @Override
    public Page<AlertData> selectAlertDataList(AlarmRecordDto alarmRecordDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Page<AlertData> page = Page.of(alarmRecordDto.getPageNum(), alarmRecordDto.getPageSize());
        page.addOrder(OrderItem.desc("create_time"));
        page = this.lambdaQuery()
                //只有当前数据相关护理员才能看见
                .eq(AlertData::getUserId,loginUser.getUserId())
                .like(ObjectUtil.isNotEmpty(alarmRecordDto.getDeviceName()), AlertData::getDeviceName, alarmRecordDto.getDeviceName())
                .eq(ObjectUtil.isNotEmpty(alarmRecordDto.getStatus()), AlertData::getStatus, alarmRecordDto.getStatus())
                .between(ObjectUtil.isNotEmpty(alarmRecordDto.getStartTime()) && ObjectUtil.isNotEmpty(alarmRecordDto.getEndTime()), AlertData::getCreateTime, alarmRecordDto.getStartTime(), alarmRecordDto.getEndTime())
                .page(page);

        return page;
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 处理设备报警数据
     * @param alertData
     */
    @Override
    public void handleAlertData(AlertData alertData) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        this.lambdaUpdate().set(AlertData::getProcessingResult,alertData.getProcessingResult())
                .set(AlertData::getProcessingTime,alertData.getProcessingTime())
                .set(AlertData::getStatus,1)
                .set(AlertData::getProcessorId,loginUser.getUserId())
                .set(AlertData::getProcessorName,loginUser.getUser().getNickName())
                .eq(AlertData::getId,alertData.getId())
                .update();
    }
}
